package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Transactional
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务，参数：{}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        //申请单的初始状态为“已发起”10
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        applicationMapper.insert(application);
        //还需要调用审批业务层，为当前申请单创建对应的审批单！
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务，参数：{}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历刚刚查出来的申请单列表，依次取出每个申请单VO,为它们补全审批人数据
        for (int i = 0; i < list.size(); i++) {
            ApplicationVO applicationVO = list.get(i);
            //处理当前的申请单对象，为它补全数据
            assginAuditUserList(applicationVO);
        }
        return list;
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请业务，参数：{}", id);
        Application application = new Application();
        application.setId(id);
        //撤销需要将当前申请单的状态改为“撤销”
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        //还需要删除此申请单对应的所有审批单数据
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务，参数：{},{}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("还车业务:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        /* 车辆状态改为"空闲",申请单状态改为"工单结束",但申请单的车辆id要设置为null */
        application.setVehicleId(null);
        //设置当前申请单的状态为"工单结束"
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.back(application);

        //还车需要把车辆状态修改为"空闲"
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicleMapper.update(vehicle);
    }

    private void assginAuditUserList(ApplicationVO applicationVO) {
        //1.准备空集合，用来保存审批人姓名与id
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        //2.根据传入的申请单id，查询当前申请单对应的审批数据，注意：一个申请可能对应多个审批！
        List<AuditVO> auditVOList =
                auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.依次遍历每个审批单对象
        for(int i = 0; i < auditVOList.size(); i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.获取当前审批单对象中的审批人id,存入上方的空集合中
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.根据审批人id,查出此人姓名，并存入上方的空集合中
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.准备一个字符串拼接工具
        StringJoiner stringJoiner = new StringJoiner(",");
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //7.将查出的审批人id赋值给传入的ApplicationVO的auditUserIdList属性
        applicationVO.setAuditUserIdList(auditUserIdList);
        //8.将转为字符串的审批人姓名字符串，赋值给传入的ApplicationVO的auditUsernameList属性
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
